﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace DDBS_Server.Models
{
    public class Sample
    {
        public string Name { get; set; }
        public string Author { get; set; }
        public string Company { get; set; }
        public string Shard { get; set; }
        public string OriginShard { get; set; }
        public List<Peak> Peaks { get; set; }
        public string Id { get; set; } // for database assigned Id

        public Sample()
        { }

        public Sample(string Name)
        {
            this.Name = Name;
        }

        public Sample(string Name, List<Peak> Peaks)
        {
            this.Name = Name;
            this.Peaks = Peaks;
        }

        public Sample(string Name, string Shard, List<Peak> Peaks)
        {
            this.Name = Name;
            this.Shard = Shard;
            this.Peaks = Peaks;
        }

        public override string ToString()
        {
            string peaksString = String.Join("; ", Peaks);
            return String.Format("Name: {0}, Shard: {1}", Name, Shard);
            //return String.Format("Name: {0}, Shard: {1} => {2}", Name, Shard, peaksString);
        }

        public string Details()
        {
            string peaksString = String.Join("; ", Peaks);
            return String.Format("{0} => {1}", Name, peaksString);
        }
        
        // for graph
        public double[] getMs()
        {
            return Peaks.Select(p => p.M).ToArray();
        }

        // for graph
        public float[] getVs()
        {
            return Peaks.Select(p => p.V).ToArray();
        }

        public bool PeaksFromCSV(string csv)
        {
            Peaks = new List<Peak>();
            try
            {
                string[] lines = csv.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);
                foreach (string line in lines)
                {
                    string[] values = line.Split(';');
                    if (values.Count() < 2) continue;
                    Peak peak = new Peak(Double.Parse(values[0]), float.Parse(values[1]));
                    Peaks.Add(peak);
                }
                return true;
            }
            catch
            {
                return false;
            }

        }
    }
}